﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Dal;
using Weight.Model;
using static Weight.Base.PhraseConfigEntity;

namespace Weight.Bll
{
    public  class BllPhrase
    {
        /// <summary>
        /// 查找数据
        /// </summary>
        /// <param name="category">类别</param>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public IEnumerable<PhraseEnity> GetPhrases(string category, string nameOrPinYin,string extFilter)
        {
            return new DalPhrase().GetPhrases(category, nameOrPinYin, extFilter);
        }

        /// <summary>
        /// 获取所有的词组
        /// </summary>
        /// <returns></returns>
        public IEnumerable<PhraseEnity> GetAllPhrases()
        {
            return new DalPhrase().GetAllPhrases();
        }

        

        /// <summary>
        /// 通过编号获取词组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PhraseEnity GetPhraseById(string id)
        {
            return new DalPhrase().GetPhraseById(id);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool  Add(PhraseEnity entity)
        {
            return new DalPhrase().Add(entity) > 0 ? true:false;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(PhraseEnity entity)
        {
            return new DalPhrase().Updae(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public bool Del(string id)
        {
            return new DalPhrase().Del(id);
        }

        /// <summary>
        /// 获取词组别名
        /// </summary>
        /// <param name="aliasTemplate"></param>
        /// <param name="orgAlias"></param>
        /// <param name="phraseMaps"></param>
        /// <returns></returns>
        public Dictionary<string, List< FieldEntity>> GetFieldPhraseAlias(IEnumerable<AliasTemplateEntity> aliasTemplate, Dictionary<string, FieldEntity> orgAlias, List<PhraseMap> phraseMaps)
        {
            Dictionary<string, List< FieldEntity>> phraseAlias = new Dictionary<string,  List< FieldEntity>>();
            if (aliasTemplate == null || orgAlias == null || orgAlias.Count == 0 || phraseMaps == null) return phraseAlias;
            foreach(var it in phraseMaps)
            {
             
                if (string.IsNullOrEmpty(it.BindField)) continue;
                FieldEntity aliasItem; 
                foreach (var item in orgAlias)
                {
                    var tmp1 = aliasTemplate.Where(p => p.Field == item.Value.Field && p.Category == it.Code).FirstOrDefault();
                    aliasItem = new FieldEntity()
                    {
                        Id = item.Value.Id,
                        Field = item.Value.Field,
                        Alias = item.Value.Alias,
                        Rank = item.Value.Rank,
                        IsShow = item.Value.IsShow
                    };
                    if (tmp1 != null)
                    {
                        aliasItem.Alias = tmp1.Alias;
                    }

                    if (it.Relations != null)
                    {
                        var tmp2 =  it.Relations.Where(p => p.Field == item.Value.Field).FirstOrDefault();
                        if(tmp2 != null)
                        {
                            aliasItem.Alias = tmp2.FieldName;
                        }
                    }

                        if (!phraseAlias.Keys.Contains(it.BindField))
                    {
                        List<FieldEntity> list = new List<FieldEntity>();
                        list.Add(aliasItem);
                        phraseAlias.Add(it.BindField, list);
                    }
                    else
                    {
                        phraseAlias[it.BindField].Add(aliasItem);
                    }
                }
            }
            

            return phraseAlias;
        }
    }
}
